﻿using System;
using System.Linq;
using System.Collections.Generic;

using BusinessObjects;
using DataObjects.EntityFramework.ModelMapper;
using System.Linq.Dynamic;

namespace DataObjects.EntityFramework.Implementation
{
    /// <summary>
    /// Entity Framework implementation of the ICustomerDao interface.
    /// </summary>
    public class EntitySupplierDao : ISupplierDao
    {
        /// <summary>
        /// Gets a specific customer.
        /// </summary>
        /// <param name="customerId">Unique customer identifier.</param>
        /// <returns>Customer.</returns>
        public Supplier GetSupplier(int supplierId)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                return Mapper.Map(context.SupplierEntities.SingleOrDefault(s => (s.SupplierId == supplierId)));
            }
        }
        /// <summary>
        /// Gets a specific customer.
        /// </summary>
        /// <param name="customerId">Unique customer identifier.</param>
        /// <returns>Customer.</returns>
        public List<Supplier> SupplierLogin(string userName,string passWord)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                var supplier = context.SupplierEntities.Where(s => s.UserName == userName && s.Password == passWord).ToList();
                return supplier.Select(s => Mapper.Map(s)).ToList();
            }
        }
        /// <summary>
        /// Gets a sorted list of all customers.
        /// </summary>
        /// <param name="sortExpression">Sort order.</param>
        /// <returns>Sorted list of customers.</returns>
        public List<Supplier> GetSuppliers(string sortExpression = "SupplierId ASC")
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                // Load single Category with all Product child records
               var supplier = context.SupplierEntities.ToList();

                // Order by (using dynamic linq) without going back to db
               // var suppliers = supplier.AsQueryable().OrderBy(sortExpression, null);

                // Return list of business objects
                return supplier.Select(s => Mapper.Map(s)).ToList();
            }
        }      


        /// <summary>
        /// Inserts a new customer. 
        /// </summary>
        /// <remarks>
        /// Following insert, customer object will contain the new identifier.
        /// </remarks>
        /// <param name="customer">Customer.</param>
        public void InsertSupplier(Supplier supplier)
        {
            var entity = Mapper.Map(supplier);

            using (var context = DataObjectFactory.CreateContext())
            {
                context.SupplierEntities.AddObject(entity);
                context.SaveChanges();

                // update business object with new version and id                
               
                supplier.SupplierId = entity.SupplierId;
                supplier.Version = Convert.ToBase64String(entity.Version);
            }
        }
        /// <summary>
        /// Updates a customer.
        /// </summary>
        /// <param name="customer">Customer.</param>
        public void UpdateSupplier(Supplier supplier)
         {
            
        }
        /// <summary>
        /// Deletes a customer
        /// </summary>
        /// <param name="customer">Customer.</param>
        public void DeleteSupplier(Supplier supplier)
        {
            
        }
    }
}
